/*
 * 创建日期 2006-6-21
 */
package com.jmcgg.freesky.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Title: FreeSky Common Java Library <br>
 * Description: FreeSky Common Java Library <br>
 * Copyright: Copyright (c) 2005~2009 www.afss.cn <br>
 * Company: Amei's FreeSky Studio <br>
 * 
 * @author <b>Jacky Chou</b> (AFSS [Amei's FreeSky Studio])
 * @version 1.2.1, 2008-08-05, Clean and optimize the source code.<br>
 *          1.2.0, 2008-02-23, Upgrade freesky.jar to 1.2.0 with Struts 2<br>
 *          1.0.0, 2006-05-30, Initial Version
 */
public class LogUtil extends FreeskyUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(LogUtil.class);

	public static String FS_LOG_FILE = "FS_LOG_FILE.LOG";
	// LEVEL: DEBUG=0; INFO=1; WARN=2; ERROR=3; FATAL=4;
	public static String LOG_LEVEL = "DEBUG";
	private static Properties prop = new Properties();

	public LogUtil() {
	}

	private static boolean load() {
		try {
			prop.clear();
			prop.load(new FileInputStream(FS_LOG_FILE));
			return true;
		} catch (FileNotFoundException e) {
			logger.error("load() - The log file can NOT be found.", e);
			// If log file is NOT found, create a new log file.
			createLogFile(FS_LOG_FILE);
			try {
				prop.load(new FileInputStream(FS_LOG_FILE));
				return true;
			} catch (IOException ex) {
				logger.error("load()", ex);
			}
		} catch (IOException e) {
			logger.error("load() - The log file can NOT be found. ", e);
		}
		return false;
	}

	public static boolean load(String fileName) {
		try {
			prop.clear();
			prop.load(new FileInputStream(fileName));
			return true;
		} catch (FileNotFoundException e) {
			logger.error("load(String) - String fileName=" + fileName, e);
			// If log file is NOT found, create a new log file.
			createLogFile(fileName);
			try {
				prop.load(new FileInputStream(fileName));
				return true;
			} catch (IOException ex) {
				System.err.print(ex.getMessage());
			}
		} catch (IOException e) {
			logger.error("load(String) - String fileName=" + fileName, e);
		}
		return false;
	}

	private static boolean store() {
		try {
			prop.store(new FileOutputStream(FS_LOG_FILE), null);
			return true;
		} catch (FileNotFoundException e) {
			logger.error("store()", e);
			// If log file is NOT found, create a new log file.
			createLogFile(FS_LOG_FILE);
			try {
				prop.store(new FileOutputStream(FS_LOG_FILE), null);
			} catch (IOException ex) {
				System.err.print(ex.getMessage());
			}
		} catch (IOException e) {
			logger.error("store()", e);
		}
		return false;
	}

	public static void debug(String msg) {
		log(0, "DEBUG", msg);
	}

	public static void info(String msg) {
		log(1, "INFO", msg);
	}

	public static void warn(String msg) {
		log(2, "WARN", msg);
	}

	public static void error(String msg) {
		log(3, "ERROR", msg);
	}

	public static void fatal(String msg) {
		log(4, "FATAL", msg);
	}

	private static void log(int logLevel, String preFix, String msg) {
		System.err.println((new Date()).toString());
		load();
		if (logLevel >= getLogLevel()) {
			prop.setProperty("ERROR_INDEX", String.valueOf(getErrIdx() + 1));
			prop.setProperty(String.valueOf(getErrIdx() % 100), "[" + preFix
					+ "][" + DateUtil.getDateTimeNoSpt() + "]" + msg);
		}
		store();
	}

	private static int getErrIdx() {
		return TypeUtil.str2Int(prop.getProperty("ERROR_INDEX"));
	}

	private static int getLogLevel() {
		if (LOG_LEVEL.equalsIgnoreCase("DEBUG"))
			return 0;
		else if (LOG_LEVEL.equalsIgnoreCase("INFO"))
			return 1;
		else if (LOG_LEVEL.equalsIgnoreCase("WARN"))
			return 2;
		else if (LOG_LEVEL.equalsIgnoreCase("ERROR"))
			return 3;
		else if (LOG_LEVEL.equalsIgnoreCase("FATAL"))
			return 4;
		else
			return 0; // set default is debug
	}

	private static boolean createLogFile(String fileName) {
		try {
			File file = new File(fileName);
			return file.createNewFile();
		} catch (IOException e) {
			logger.error(
					"createLogFile(String) - Creating Log File fails - String fileName="
							+ fileName, e);
		}
		return false;
	}
}
